function clearAllDialogMarkup() {
    $("body").find("div[role=dialog]").html("").remove();
}
///clear this markup
function clearDialogMarkup(dlg) {
    $(dlg).parent().remove().end().remove();

}
(function ($) {
    $.fn.openDlgFrame = function (settings) {
        var holder = $(this);
        var defaults = {
            dlg: "x",dlgID:"",titleCaption: "",
            width: 700,height: 200,
            url: "",sendData: "",
            resizable: true,Modal: true,
            autoDIV: true,
            closeOther: true,
            closeCallback: "",
            openCallback: "",
            zIndex: 999,
            disableClose : false
        }
        var options = jQuery.extend(defaults, settings);
        options = setSizeDlg(options);

        var lastScroll = $(".ui-layout-center:first").scrollTop();
        if (options.autoDIV) {
            if ($("body").find("div#" + options.dlg).size() == 0) {
                $("body").append('<div id="' + options.dlg + '"></div>');
            }
        }
        var dlgTemp = $('#' + options.dlg);
        if (!options.Modal) {
            options.Modal = false;
        }
        //clear all
        if (options.closeOther) {
            clearAllDialogMarkup();
        }
        dlgTemp.dialog({
            autoOpen: false,
            modal: options.Modal,
            //show: {effect: "fadeIn",duration: 100},
            show: {effect: "blind",duration: 200},
            //show: 'clip', 
            //hide: {effect: "fadeOut",duration: 100},
            hide: {effect: "blind",duration: 200},
            //hide: 'clip',
            position: { my: "center", at: "center", of: window },
            width: options.width, height: options.height,
            title: options.titleCaption, resizable: options.resizable,
            zIndex: options.zIndex,
            bgiframe: true,
            close: function (event, ui) {
                //clear dialog on close
                if (options.autoDIV) {
                    clearDialogMarkup("div#" + options.dlg);
                }
                //calback
                if (options.closeCallback) { eval(options.closeCallback) }
                //scroll to last position
                $(".ui-layout-center:first").scrollTop(lastScroll);
                //
            },
            open: function (event, ui) {
                //$(this).html('<div class="openPage">Opening page please wait...');
                if (options.disableClose == true) { $(".ui-dialog-titlebar-close:last").hide(); }
                var opt = optionLoad();
                $("#" + options.dlg).block(opt); //end block del
                $.post(options.url, options.sendData, function (data) {
                    $('#' + options.dlg).hide().fadeIn(1000).css({ 'overflow': 'auto' });
                    $('#' + options.dlg).html(data);
                    $("#" + options.dlg).ajaxStop(function () {
                        $("#" + options.dlg).unblock(); //unblock and block
                    });
                    if (options.openCallback) { eval(options.openCallback) }
                });

            }
        });
        dlgTemp.dialog('open');
        //ปิด dialog Frame ทั้งหมด
    };
})(jQuery);


function setSizeDlg(options)
{
    if(options.url == "/BasedPattern/ShowDetail")
    {
        options.width=1200;options.height=630;
    }
    return options;
}